package com.hr;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.espirit.eap.pagelayout.ModelService;
import com.espirit.eap.pagelayout.SearchModelQuery;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.ParameterMap;
import com.googlecode.cswish.struts.spring.Executor;

@Service
public class SchedualScopeService {
	
	@Resource
	ModelService modelService;
	
	@Resource
	SchedualDefineService schedualDefineService;

	
	public void registerModelSearchType() {
		Executor<PageInfo, SearchModelQuery> schedualScopeSearch = new Executor<PageInfo, SearchModelQuery>() {
			@Override
			public PageInfo execute(SearchModelQuery searchModelQuery) {
				String name = searchModelQuery.name;
				String labelProperty = searchModelQuery.labelProperty;
				String query = searchModelQuery.query;
				String filterProperty = searchModelQuery.filterProperty;
				String filter = searchModelQuery.filter;
				
				ParameterMap model = new ParameterMap();
				model.setModelName(name);
				if (query != null && query.length() > 0) {
					String quoteQuery = StringUtils.replace(query, "&", "\\&");
					model.setParameter(labelProperty, quoteQuery);
					model.setParameter("pinyin", "|" + quoteQuery);
				}
				if (filterProperty != null && filter != null && filter.length() > 0) {
					model.setParameter(filterProperty, filter);
				}
				boolean disablePermission = false;
				String sort = null;
				String dir = null;
				
				int start = (searchModelQuery.pageNo - 1) * searchModelQuery.pageSize;
				int limit = searchModelQuery.pageSize;
				
				Integer deptId = Integer.valueOf(filter);
				SchedualDefine schedualDefine = schedualDefineService.getSchedualDefineByDeptId(deptId);
				if (schedualDefine != null){
					List data = schedualDefine.getSchedualScopes();
					PageInfo pageInfo = new PageInfo();
					pageInfo.setData(data);
					return pageInfo;
				} else {
					return null;
				}
				
//				PageInfo pageInfo = cacheSearchService.search(model, start, limit, sort, dir, false, true,
//						null, null, searchModelQuery.statCount, disablePermission);
//				return pageInfo;
			}
		};
		
		modelService.registerSearchType(SchedualScope.class.getName(), schedualScopeSearch);
	}
	
}
